package day_02.heap;

public class Main {
    public static void main(String[] args) {
        int[] arr = {5, 4, 3, 9, 10, 2, 1, 8};
        sort(arr);
        for(int i: arr){
            System.out.print(i + " ");
        }
    }

    private static void sort(int[] arr) {
        quickSort(arr, 0, arr.length-1);
    }

    private static void quickSort(int[] arr, int l, int r) {
        if(l >= r) return;
        int p = partition(arr, l, r);
        quickSort(arr, l, p-1);
        quickSort(arr, p+1, r);
    }

    public static int partition(int[] arr, int l, int r){
        int v = arr[l];
        int j = l;
        for (int i = l+1; i <= r; i++) {
            if(arr[i] < v){
                swap(arr, i, j+1);
                j++;
            }
        }
        swap(arr, l, j);
        return j;
    }

    private static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
